t = int(input())
b = 0
if t < 999 or t > 9999:
    print("0")
else:
    def p_n_p(a):
        k = 0
        for i in range(2, a // 2 + 1):
            if a % i == 0:
                k = k + 1
        if k <= 0:
            return True
        else:
            return False
    t4 = t % 10
    t3 = t % 100 // 10
    t2 = t // 100 % 10
    t1 = t // 1000
    x3 = (t // 10) % 100
    x2 = t % 100
    x1 = t// 100
    y1 = t % 1000
    y2 = t // 10
    if p_n_p(t1) == True and b < t1:
        b = t1
    if p_n_p(t2) == True and b < t2:
        b = t2
    if p_n_p(t3) == True and b < t3:
        b = t3
    if p_n_p(t4) == True and b < t4:
        b = t4
    if p_n_p(x1) == True and b < x1:
        b = x1
    if p_n_p(x2) == True and b < x2:
        b = x2
    if p_n_p(x3) == True and b < x3:
        b = x3
    if p_n_p(y1) == True and b < y1:
        b = y1
    if p_n_p(y2) == True and b < y2:
        b = y2
    if p_n_p( t ) == True and b < t:
        b = t
print(b)